package com.youlai.boot.modules.comment.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.comment.model.CommentForm;
import com.youlai.boot.modules.comment.model.CommentVO;
import com.youlai.boot.modules.comment.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "评论接口")
@RestController
@RequestMapping("/api/v1/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    @Operation(summary = "评论分页列表")
    @GetMapping("/page")
    public PageResult<CommentVO> listComments(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long rootId,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CommentVO> result = commentService.getCommentPage(userId, rootId, status, pageNum, pageSize);
        return PageResult.success(result);
    }

    @Operation(summary = "评论详情")
    @GetMapping("/{id}")
    public Result<CommentVO> getComment(@PathVariable Long id) {
        CommentVO comment = commentService.getCommentById(id);
        return Result.success(comment);
    }

    @Operation(summary = "添加评论")
    @PostMapping
    public Result<Long> addComment(@Valid @RequestBody CommentForm comment) {
        Long id = commentService.saveComment(comment);
        return Result.success(id);
    }

    @Operation(summary = "修改评论")
    @PutMapping("/{id}")
    public Result<Boolean> updateComment(
            @PathVariable Long id,
            @Valid @RequestBody CommentForm comment) {
        comment.setId(id);
        commentService.updateComment(comment);
        return Result.success(true);
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteComment(@PathVariable Long id) {
        boolean result = commentService.deleteComment(id);
        return Result.judge(result);
    }

    @Operation(summary = "点赞评论")
    @PostMapping("/{id}/like")
    public Result<Boolean> likeComment(@PathVariable Long id) {
        boolean result = commentService.likeComment(id);
        return Result.judge(result);
    }
}